using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace SqlPrompt
{
	public class SerializationUtil
	{
		public static string SerializeToXml(object obj)
		{
			XmlSerializer serializer = new XmlSerializer(obj.GetType());
			using (StringWriter writer = new StringWriter(new StringBuilder()))
			{
				serializer.Serialize(writer, obj);
				return writer.ToString();
			}
		}

		public static void SerializeToXmlFile(object obj, string filePath)
		{
			XmlSerializer serializer = new XmlSerializer(obj.GetType());
			using (StreamWriter writer = new StreamWriter(filePath))
			{
				serializer.Serialize(writer, obj);
			}
		}

		public static object DeserializeFromXml(string xml, Type type)
		{
			using (TextReader reader = new StringReader(xml))
			{
				XmlSerializer serializer = new XmlSerializer(type);
				return serializer.Deserialize(reader);
			}
		}

		public static object DeserializeFromXmlFile(string filePath, Type type)
		{
			using (StreamReader reader = new StreamReader(filePath))
			{
				XmlSerializer serializer = new XmlSerializer(type);
				return serializer.Deserialize(reader);
			}
		}
	}
}
